package com.xquant.trade.rocketmq;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import com.xquant.common.core.constant.ErrMsgConstant;
import com.xquant.common.core.exception.ServiceException;
import com.xquant.trade.trade.mapper.TradeMapper;
import com.xquant.trade.trade.service.DurationCalcService;
import com.xquant.trade.trade.service.SettleService;
import com.xquant.trade.trade.service.impl.DurationServiceImpl;
import com.xquant.trade.utils.DateUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;
import java.util.Map;


/**
 * @author zes
 * @createDateTime 2022/5/12 1:33 上午
 */
@Service
@RefreshScope
@RocketMQMessageListener(topic = "${rocketmq.trade-topic}", selectorExpression = "*", consumerGroup = "${rocketmq.consumer.group}")
public class RocketMqTradeConsumer implements RocketMQListener<JSONObject> {
    protected static final Logger logger = LoggerFactory.getLogger(RocketMqTradeConsumer.class);

    @Autowired
    private DurationServiceImpl durationService;
    @Autowired
    private DurationCalcService durationCalcService;
    @Autowired
    private TradeMapper tradeMapper;
    @Autowired
    private SettleService settleService;

    @Override
    public void onMessage(JSONObject jsonObject) {
        logger.info(jsonObject.getStr("iCode") + " 到达队列监听器（rocketmq）");
        String current = settleService.getBlcDate();
        logger.info("到达队列监听器");
        Map param = (Map) JSONUtil.parse(jsonObject);
        try {
            String terminationDate = (String) param.get("terminationDate");
            String issueDate = (String) param.get("issueDate");
            // 删除到所有的dutation的存续状态
            tradeMapper.removeAllEventStatus(param);
            // 删除掉Structure的存续状态
            tradeMapper.removeEventStatus(param);
            if (issueDate.compareTo(current) < 0) {
                List<String> days = DateUtils.getRangeDate(issueDate, current);
                days.stream().forEach(data -> {
                    try {
                        param.put("calcDate", data);
                        //估值计算
                        durationService.calcTrade4OtcOptionByPage(param);
                        //存续计算
                        durationCalcService.calcDuration(param, data);
                        //到期计算
                        durationCalcService.execExpireContract(param, data);
                    } catch (ParseException e) {
                        throw new ServiceException(ErrMsgConstant.CALC_TASK_ERR,e.getMessage());
                    }
                });
            }else {
                param.put("calcDate", current);
                //估值计算
                durationService.calcTrade4OtcOptionByPage(param);
                //存续计算
                durationCalcService.calcDuration(param, current);
                //到期计算
                durationCalcService.execExpireContract(param, current);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

